package com.zian.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zian.context.BaseContext;
import com.zian.domain.dto.PageDTO;
import com.zian.domain.dto.ShoppingDTO;
import com.zian.domain.po.SysShoppingCartItem;
import com.zian.domain.query.PageQuery;
import com.zian.domain.vo.ShoppingQueryVO;
import com.zian.enums.IsDeletedEnum;
import com.zian.exception.BizIllegalException;
import com.zian.exception.DbException;
import com.zian.mapper.SysShoppingCartItemMapper;
import com.zian.service.ISysShoppingCartItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2024-09-08
 */
@Slf4j
@Service
public class SysShoppingCartItemServiceImpl extends ServiceImpl<SysShoppingCartItemMapper, SysShoppingCartItem> implements ISysShoppingCartItemService {


    @Override
    public List<ShoppingQueryVO> getSelectedItems(List<Long> ids) {
        if (ids.isEmpty()) return CollUtil.empty(List.class);
        Long currentId = BaseContext.getCurrentId();
        List<ShoppingQueryVO> vos = getBaseMapper().getSelectedItems(currentId, ids);
        if (CollUtil.isEmpty(vos)) return CollUtil.empty(List.class);
        return vos;
    }

    @Override
    public Integer getShoppingCartItemCount() {
        Long currentId = BaseContext.getCurrentId();
        // 统计当前用户的购物车的商品数量
        Integer count = getBaseMapper().getShoppingCartItemCount(currentId);
        if (count == null) {
            return 0;
        }
        return count;

    }

    @Override
    public void addShoppingCartItem(Long goodsId) {
        Long currentId = BaseContext.getCurrentId();
        SysShoppingCartItem one = lambdaQuery()
                .eq(SysShoppingCartItem::getGoodsId, goodsId)
                .eq(SysShoppingCartItem::getUserId, currentId)
                .eq(SysShoppingCartItem::getIsDeleted, IsDeletedEnum.NO)
                .one();
        if (one != null) {
            if (one.getGoodsCount() >= 5) {
                throw new BizIllegalException("商品数量不能大于5");
            }
            // 修改商品数量
            boolean b = lambdaUpdate()
                    .eq(SysShoppingCartItem::getId, one.getId())
                    .set(SysShoppingCartItem::getGoodsCount, one.getGoodsCount() + 1)
                    .set(SysShoppingCartItem::getUpdateTime, LocalDateTime.now())
                    .update();
            if (!b) throw new DbException("修改购物车失败");
            return;
        }
        SysShoppingCartItem cartItem = new SysShoppingCartItem();
        cartItem.setGoodsCount(1);
        cartItem.setGoodsId(goodsId);
        cartItem.setUserId(currentId);
        cartItem.setIsDeleted(IsDeletedEnum.NO);

        boolean b = save(cartItem);
        if (!b) throw new DbException("添加购物车失败");
    }

    @Override
    public void deleteShoppingCartItem(Long id) {
        Long currentId = BaseContext.getCurrentId();
        SysShoppingCartItem cartItem = lambdaQuery()
                .eq(SysShoppingCartItem::getId, id)
                .eq(SysShoppingCartItem::getUserId, currentId)
                .one();
        if (cartItem == null) throw new DbException("不存在购物项");
        boolean b = lambdaUpdate()
                .eq(SysShoppingCartItem::getId, id)
                .set(SysShoppingCartItem::getIsDeleted, IsDeletedEnum.YES)
                .set(SysShoppingCartItem::getUpdateTime, LocalDateTime.now())
                .update();
        if (!b) throw new DbException("删除购物项失败");
    }

    @Override
    public void updateGoodsCount(ShoppingDTO dto) {
        Long currentId = BaseContext.getCurrentId();
        SysShoppingCartItem cartItem = getById(dto.getId());
        if (cartItem == null) throw new BizIllegalException("不存在购物项");
        if (cartItem.getIsDeleted().equals(IsDeletedEnum.YES)) throw new BizIllegalException("不存在购物项");
        if (!cartItem.getUserId().equals(currentId)) throw new BizIllegalException("非法操作");
        if (cartItem.getGoodsCount() > 5) throw new BizIllegalException("商品数量不能大于5");

        boolean b = lambdaUpdate()
                .eq(SysShoppingCartItem::getId, dto.getId())
                .set(SysShoppingCartItem::getGoodsCount, dto.getGoodsCount())
                .set(SysShoppingCartItem::getUpdateTime, LocalDateTime.now())
                .update();
        if (!b) throw new DbException("更新购物项失败");
    }

    @Override
    public List<ShoppingQueryVO> getShoppingCartItemList() {
        Long currentId = BaseContext.getCurrentId();
        List<ShoppingQueryVO> vos = getBaseMapper().getShoppingCartItemList(currentId);
        if (vos.isEmpty()) return CollUtil.empty(List.class);
        return vos;
    }
}
